package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper ;


    public PageInfo query(QueryObject queryObject) {
        //调用PageHelper方法，设置参数
        PageHelper.startPage(queryObject.getCurrentPage(), queryObject.getPageSize());
        //调用mapper接口的方法
        List<User> list = userMapper.selectAll(queryObject);
        return new PageInfo(list);
    }

    @Override
    public User getUesrData(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void concern(Long userId) {
        User currentUser = UserContext.getCurrentUser();
        //如果登录了可以进行关注,否则提示去登录
        if(currentUser!=null){
            Long fansId =currentUser.getId();
            //进行注册功能
            userMapper .concern(userId,fansId);
        }else{
            throw new RuntimeException("请先登录");
        }

    }

    @Override
    public boolean isConcern(Long userId) {
        User currentUser = UserContext.getCurrentUser();
        //如果登录了可以进行关注,否则提示去登录
        if(currentUser!=null){
            Long fansId =currentUser.getId();
            //进行注册功能
            int result = userMapper .isConcern(userId,fansId);
            //如果已经关注,返回false
            if(result>0){
                return false;
            }
        }
        return true;

    }

    @Override
    public List<User> getfansOrWatchs(Long id, String type) {
        List<User> list = new ArrayList<>();
        if ("fans".equals(type)){
            list = userMapper.getFans(id);
        }else if("watch".equals(type)){
            list = userMapper.getWatchs(id);
        }else{
            return null;
        }
        return list;
    }

    @Override
    public void saveOrUpdate(User u) {
        //判断是新增操作还是修改操作
        if(u.getId()==null){
            //设置一些默认值给它
            u.setCoverImgUrl("/img/user/bg.jpeg");//设置封面图片
            u.setHeadImgUrl("/img/user/head.jpg");//设置头像图片
            u.setGender(User.GENDER_NONE);//设置性别信息
            u.setPlace("中国");//地区信息
            u.setSign("好男人就是我，我就是龙胆紫");//设置个性签名
            //设置完默认值之后调用mapper接口的方法，新增一个用户
            userMapper.insert(u);
        }else{
            userMapper.updateByPrimaryKey(u);
        }
    }

    @Override
    public void register(User u) {
        User user = userMapper.checkEmail(u.getEmail());
        if(user!=null){
            //说明数据库中已经存在这个用户了
            throw new RuntimeException("此用户已存在");
        }
        //否则的话就是没有这个用户，记忆可以进行新增操作
        saveOrUpdate(u);
    }

    @Override
    public User login(String email, String password) {
        //调用mapper接口中的方法，查询该用户是否存在
        User u = userMapper.selectByInfo(email,password);
        //因为有可能登录失败，所以要进行判断
        if(u==null){
            throw new RuntimeException("邮件或密码不正确");
        }
        return u;
    }


}
